// service

var service = {

	state: {
		//*date: (new Date()).formatDate(),
		vst_dt: (new Date('2011-09-03')).formatDate(),
		emp_id: null,
		mnu_id: null,
	},

	funcs: {
		calc: function(e) {
			console.log( 'service.funcs.calc starting calc on '+e.target );
			var el = e.target;

			// from ts
			var from_tm = util.fixTime(Post.colSiblingValue(el,'from_tm'));
			if( from_tm ) {
				Post.colSiblingSetValue( el, 'from_ts', service.state.vst_dt + ' ' + from_tm );
				Post.colSiblingSetValue( el, 'from_tm', from_tm );
			}
			else	Post.colSiblingSetValue( el, 'from_ts', '' );

			// thru ts
			var thru_tm = util.fixTime(Post.colSiblingValue(el,'thru_tm'));
			if( thru_tm ) {
				Post.colSiblingSetValue( el, 'thru_ts', service.state.vst_dt + ' ' + thru_tm );
				Post.colSiblingSetValue( el, 'thru_tm', thru_tm );
			}
			else	Post.colSiblingSetValue( el, 'thru_ts', '' );
		},
		// set ts to current time on double click
		clickTm: function(e) {
			e.target.value = (new Date()).formatTime();
			// init change event from here
			var change = document.createEvent("Event");
			change.initEvent('change',true,true);
			e.target.dispatchEvent(change);
		}
	},

	// one visit
	visit: function( vst_id ) {

		// event passed as arg
		if( vst_id && vst_id instanceof Event )	vst_id = service.state.vst_id;
		else if( vst_id == undefined )		vst_id = service.state.vst_id;
		else					service.state.vst_id = vst_id;

		console.log( 'new visit' );

		// one visit
		var vst = new Render( 'vst', { dfltRcd:{vst_dt:service.state.vst_dt}, dd:'vst', extraNo:((vst_id)?0:1), select:{pkval:vst_id},
			formRender: function() { return vst.form( 'rel', 'update:form', 'tup', vst.hidden('vst_dt'),
				vst.div( vst.label( 'Nationality', vst.select( 'nationality' ) ),
					 ' \u00a0 ', vst.label( 'Void', vst.checkbox('voided') ), vst.label( 'Paid', vst.checkbox('paid') ) ),
				vs.formRender(),
				vst.div(  '.nbk', vst.button( vst.submit, 'Submit' ), vst.button( vst.cancel, 'Cancel' ) ) ) },
			cancel: function(e) { e.stopPropagation(); e.preventDefault(); util.popout(); },
			submit: function(e) { e.stopPropagation(); e.preventDefault(); console.log('saving'); }
		} );
		// pull first (and only) record in
		vst.next();

		// services in a visit
		var vs = new Render( 'vs', {
			dd:'svc',
			pnt: { render:vst, pntCol:'vst_id', childCol:'vst_id' },
			select: { tbl_v:'service_raw' },
			extraNo:((vst.r('void')!=1)?1:0),
			middleTrue: function(e) { console.log( 'column middle true: '+e.currentTarget ); },
			middleFalse: function(e) { console.log( 'column middle false: '+e.currentTarget ); },
			changeTrue: function(e) { console.log( 'column change true: '+e.currentTarget ); },
			changeFalse: function(e) { console.log( 'column change false: '+e.currentTarget ); },
			dfltRcd: { from_ts: (new Date()).formatDT(), from_tm: (new Date()).formatTime() },
			formRender: function() { return vs.div( 'rel', 'update:parent', vs.rows() ) },
			rowRender: function() { return vs.div( 'tup',
				vs.div( vs.label( 'Menu Item', vs.hidden('mnu_id'), vs.text('mnu_nm','lookup','e.change:-changeFalse', 'e.change:+changeTrue'), 'e.change:-changeFalse', 'e.change:+changeTrue' ) ),
				vs.div( vs.label( 'Employee', vs.hidden('emp_id'), vs.text('emp_nm','lookup') ) ),
				vs.div( vs.label( 'From', vs.hidden('from_ts'),
						vs.time('from_tm', 'e.dblclick:service.funcs.clickTm', 'e.change:service.funcs.calc') ),
					' \u00a0 ', vs.label( 'Thru', vs.hidden('thru_ts'),
						vs.time('thru_tm', 'e.dblclick:service.funcs.clickTm', 'e.change:service.funcs.calc') ) ),
				vs.div( vs.label( 'Qty', vs.number('qty') ),
					' \u00a0 ', vs.label( 'Price', vs.span( 'att:price' ) ),
					' \u00a0 ', vs.label( 'Total', vs.span( 'att:ttl_price' ) ) ) ); }
		} );

		util.popup( vst.h4( ((vst_id)?'Edit Visit '+svc_id:'New Visit') ), vst.formRender() );
	},
	service: function( vst_dt ) {
		util.popout();

		console.log( 'service: vst_dt='+vst_dt+', service.state.vst_dt='+service.state.vst_dt );
		if( vst_dt && !(vst_dt instanceof Event) ) service.state.vst_dt = vst_dt;

		var sd = new Render( 'sd', {rcds:[{vst_dt:service.state.vst_dt}],
			formRender: function() { return sd.form( 'rel', '.l', sd.div( 'tup', sd.b('for Date'),
				sd.date( 'vst_dt', 'datatype:date', 's.marginLeft:5px', 's.width:92px', {required:true, verify:true,'e.change':function(e) {
					var dt = e.target.value;
					console.log( 'reset for '+dt );
					if( dt == '' )  nonBlockAlert( 'must have valid date' );
					else service.service(dt);
				} } ),
				sd.h1( 'Daily Services - ', 's.marginLeft:10%', 's.marginTop:0', 's.MarginBottom:0', 's.display:inline-block',
					sd.a( '#', service.visit, 'New' ) )
			) ) }
		} );

		// listing of services
		var svcs = new Render( 'svc', { select:{key:"vst_dt='"+service.state.vst_dt+"'"}, orderBy:'vst_id, from_ts',
			formRender: function() { return svcs.form( 'rel', 'update:col', 'request:row', svcs.table(
				svcs.thead( svcs.tr( svcs.ths( 'Visit', 'Nationality', 'Menu Item','By','Void','From','Thru','Price','Qty','Total','GTotal','Paid') ),
				svcs.tbody( svcs.rows() ) ) ) ) },
			rowRender: function() { return svcs.tr( 'tup',
				svcs.td( svcs.b( 'GO' ) ),
				svcs.td( svcs.select( 'nationality' ) ),
				svcs.td( svcs.hidden('mnu_id'), svcs.text('mnu_nm','lookup') ),
				svcs.td( svcs.hidden('emp_id'), svcs.text('emp_nm','lookup') ),
				svcs.td( svcs.checkbox('void') ),
				svcs.td( svcs.hidden('from_ts'), svcs.time('from_tm') ),
				svcs.td( svcs.hidden('thru_ts'), svcs.time('thru_tm') ),
				svcs.td( 'att:price' ),
				svcs.td( svcs.select('qty') ),
				svcs.td( 'att:total' ),
				svcs.td( 'att:amount' ),
				svcs.td( '.c', svcs.checkbox('paid') ) ); }
		} );

		Render.replaceAll( contentDom, sd.formRender(), svcs.formRender() );
	}
};

